import axios from 'axios'
import {actionFunc as loadingActionFunc} from '@/components/Loading/store'
import {store} from '@/store'

export default class http {

    static ajax(option){
        console.log('option:',option)
        const baseUrl = 'https://easy-mock.com/mock/5c2f221c7106f779e7eac9d0/router'
        const timeout = 5000
        console.log(option.showLoading)
        if(option.showLoading !== false ) { //打开加载动画
            console.log('enter')
            store.dispatch(loadingActionFunc.changeShowStatus(true))
        }
        return new Promise((resolve, reject)=>{
            axios({
                url: option.url,
                method: option.method || 'get',
                params: option.params || '',
                baseURL: baseUrl,
                timeout: timeout,
            }).then((response)=>{
                if(option.showLoading !== false ) { //关闭加载动画
                    store.dispatch(loadingActionFunc.changeShowStatus(false))
                }
                if( response.status === 200){
                    const res = response.data
                        resolve(res)
                }else{
                    switch (response.status) {
                        case 404:
                            //处理错误
                            break;
                        default:


                    }
                    reject(response.data)
                }
            })
        })
    }
}

